import Cookies from 'js-cookie'
import jwtDecode from 'jwt-decode'

let TokenKey = ''
if (process.env.VUE_APP_ENV === 'prod') {
  // 生产
  TokenKey = 'user-token'
} else {
  // 开发
  TokenKey = 'dev-user-token'
}

export function getToken() {
  return Cookies.get(TokenKey) || sessionStorage.getItem('token')
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getCurrentUser() {
  const access_token = Cookies.get(TokenKey)

  if (access_token != null) {
    const decode = jwtDecode(access_token)

    return decode.username
  }
}

/**
 * 从token获取用户信息
 * @returns
 */
export function getUserInfo() {
  const access_token = Cookies.get(TokenKey)
  if (access_token != null) {
    const decode = jwtDecode(access_token)

    return decode
  }
}
